<?php
/**
 * created by zzy
 * date: 2017/10/20 10:04
 */

namespace App\Eloquent\Ygt;

class User extends DbEloquent{

    protected $table    = 'ygt_user';

    public static function getLoginInfoNew( $mobile, $password ){
        $status             = $user = false;
        $fields = [
            'id','company_id','mobile','mobile_hide','truename','truename_hide','salt','password'
        ];

        $user = self::where('mobile','=',$mobile)->orWhere('truename','=',$mobile)->first($fields);

        if( $user ){
            $user = $user->toArray();
            $userPassword   = $user['password'];
            $passwordMd5    = self::passwordMd5( $password, $user['salt'] );
            if( $passwordMd5 === $userPassword ){
                $status     = true;
            }
        }
        return ['status'=>$status, 'user'=>$user];
    }

    public static function getLoginInfo( $where, $password ){

        $status             = $user = false;
        $fields             = 'id,company_id,mobile,mobile_hide,truename,truename_hide,salt,password';
        $info               = self::getInfo( $where,$fields );
        if( $info ){
            $user           = $info->toArray();
            $userPassword   = $user['password'];
            $passwordMd5    = self::passwordMd5( $password, $user['salt'] );
            if( $passwordMd5 === $userPassword ){
                $status     = true;
            }
        }
        return ['status'=>$status, 'user'=>$user];
    }
    public static function passwordMd5( $password, $salt='' ){
        $password_str       = $password.$salt;
        return md5( $password_str );
    }
    //工厂或客商添加客户时把对应的手机号注册为用户
    //type==1 工厂添加客户时的关联账号
    //type==2 工厂添加供应商时的联系人手机号
    //返回值
    //status==0 失败
    //status==1 不存在插入成功
    //status==2 已存在取值成功
    public static function registerByType($mobile,$username,$type=1)
    {
        $status         = $userId = 0;
        $message        = '';
        switch($type)
        {
            case 1:
                $defaultArr     = config('ygt.sale_arr');
                $companyId      = $defaultArr['company_id'];
                break;
            case 2:
                $defaultArr     = config('ygt.seller_arr');
                $companyId      = $defaultArr['company_id'];
                break;
            case 3:
                $defaultArr     = config('ygt.waste_buyer_arr');
                $companyId      = $defaultArr['company_id'];
                break;
            default:
                $message        = '不存在的类型';
                $result         = ['status'=>$status,'message'=>$message,'user_id'=>$userId];
                return $result;
        }
        $where                  = ['mobile'=>$mobile];
        $userInfo               = self::getInfo($where);
        if(!$userInfo){
            //注册账号
            $salt               = mt_rand( 100000, 999999 );
            $password           = '123456';
            $passwordMd5        = self::passwordMd5( $password, $salt );
            $userData           = [
                'company_id'=>$companyId,
                'mobile'=>$mobile,
                'truename'=>$username,
                'password'=>$passwordMd5,
                'salt'=>$salt
            ];
            $status             = 1;
            $userId             = self::insertOneData( $userData, 'id' );
            if(!$userId)
            {
                $status         = $userId = 0;
                $message        = '关联账号注册失败';
            }
        }else{
            $status             = 2;
            $userId             = $userInfo->id;
            $userCompanyId      = $userInfo->company_id;
            if($userCompanyId==0){
                $data           = ['company_id'=>$companyId];
                $updateResult   = self::updateOneData($data);
                if(!$updateResult)
                {
                    $status     = $userId = 0;
                    $message    = '关联账号添加失败';
                }
            }else{
                if($userCompanyId!=$companyId)
                {
                    $status     = $userId = 0;
                    $message    = '该手机号码已注册为其它角色';
                }
            }
        }
        $result                 = ['status'=>$status,'message'=>$message,'user_id'=>$userId];
        return $result;
    }
}